<template>
  <div>
    <!-- <div>我是子组件</div> -->
    <!-- show-checkbox // 是否显示选择框 -->
    <!-- check-strictly="true" //可以关闭父子关联
           default-expand-all="true" // 默认展开 -->

    <el-tree
      ref="refTree"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      :check-strictly="true"
      :default-expand-all="true"
      node-key="id"
    />
    <!-- :load="loadNode"
      lazy
      @check-change="handleCheckChange" -->
    <el-button size="small" @click="hClose">取消</el-button>
    <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
    this.loadRoleDetail()
  },
  methods: {
    hClose() {
      this.$emit('close')
    },
    async loadPermissionList() {
      const res = await getPermissionList()
      console.log(res)
      this.list = tranListToTreeData(res.data)
    },
    async loadRoleDetail() {
      const ress = await getRoleDetail(this.roleId)
      console.log(ress)
      this.$refs.refTree.setCheckedKeys(ress.data.permIds)
    },
    async hSubmit() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      console.log('更新permIds', permIds)
      console.log('角色ID', this.roleId)
      const res = await assignPerm({ id: this.roleId, permIds })
      console.log('保存权限', res)
    }
  }
}
</script>

<style>

</style>
